package com.bauer.java.pdf;

import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PdfDemo {


    public static String decodeQrimage(BufferedImage image) {
        String resultMsg = null;

        try {
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            resultMsg = result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultMsg;
    }

    public static void main(String[] args) {
        File file = new File("/Users/bauer/resources/test.pdf");
        PDDocument pdDocument = null;
        String qrResult = null;

        try {
            pdDocument = PDDocument.load(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int pages_size = pdDocument.getNumberOfPages();
        int j = 0;

        for (int i = 0; i < pages_size; i++) {
            PDPage page = pdDocument.getPage(i);
            PDResources resources = page.getResources();
            Iterable xobjects = resources.getXObjectNames();
            if (xobjects != null) {
                Iterator imageIter = xobjects.iterator();
                while (imageIter.hasNext()) {
                    COSName key = (COSName) imageIter.next();
                    if (resources.isImageXObject(key)) {
                        try {
                            PDImageXObject image = (PDImageXObject) resources.getXObject(key);


                            qrResult = decodeQrimage(image.getImage());

                            if (qrResult != null) {
                                break;
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        //image count
                        j++;
                    }
                }
            }
        }
        System.out.println(qrResult);
    }
}
